package com.simple.middle.test.redis.util;

import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目: pagination
 *
 * 功能描述:
 *
 * @author: WuChengXing
 *
 * @create: 2023-05-26 11:39
 **/
public class HyperLogLogRedisUtils {

    @Resource
    private RedisTemplate redisTemplate;

    private final HyperLogLogOperations hyperLogLogOperations = redisTemplate.opsForHyperLogLog();

    /**
     * 求总和
     *
     * @param key
     * @return
     */
    public Long pfCount(String key) {
        return hyperLogLogOperations.size(key);
    }

    /**
     * 求总和-allkey
     *
     * @param keys
     * @return
     */
    public Long pfCount(List<String> keys) {
        return hyperLogLogOperations.size(keys);
    }

    /**
     * 批量添加元素
     *
     * @param key
     * @param values
     * @return 按序 1表示添加成功，0表示添加失败
     */
    public Long pfAdd(String key, List<String> values) {
        return hyperLogLogOperations.add(key, values);
    }
}
